iT邦幫忙

2025 iThome 鐵人賽

DAY 10
0
Software Development

我們與Maven的距離系列 第 24

Day23 - Library JAR Deploy to Nexus Server

  • 分享至 

  • xImage
  •  

前言

Library JAR就是我們最一般的JAR,存粹開發出來讓其他套件使用,它不會包含其它依賴的JAR在其中,既然是要給其它人使用,那麼他就需要部署到Remote Repository,在公司裡一般我們就會部署到Nexus Server讓其他團隊可以使用,今天我們就來談談這個部分。

Nexus install

download nexus server
https://ithelp.ithome.com.tw/upload/images/20251008/20128084qmU3koScnj.png
install as serviceH:\Java\nexus-3.84.1-01-win-x86_64\nexus-3.84.1-01\bin\install-nexus-service.bat
access web browserlocalhost:8081
https://ithelp.ithome.com.tw/upload/images/20251008/20128084VU5ppATkyO.png
change default password
https://ithelp.ithome.com.tw/upload/images/20251008/20128084rB8VVGlJas.png
choose enable anonymous
https://ithelp.ithome.com.tw/upload/images/20251008/20128084BF34EcFdj3.png
default repository as follow
https://ithelp.ithome.com.tw/upload/images/20251008/20128084rmT9LNXaHP.png

Nexus Repository Type

1. Proxy Repository(Remote的代理倉庫)
作為遠端倉庫的本地快取代理,例如maven repository或是alibaba repository,maven-central,在setting/Repositorys/maven-central可以設定remote repository
https://ithelp.ithome.com.tw/upload/images/20251008/20128084d49UuJThW3.png
2. Hosted Repository(託管倉庫)
作為公司內部發佈套件存放的地方,例如maven-release、maven-snapshots
3. Group Repository(群組倉庫)
透過Group的設定可以將多個repository group起來作為統一入口,例如maven-public
https://ithelp.ithome.com.tw/upload/images/20251008/201280842REX3Ev40p.png

Maven setting

enter ur local maven/conf/setting.xml in mirrors to set as follow

<mirrors>
  <!-- 預設禁用http 這邊要設為false-->
  <mirror>
      <id>maven-default-http-blocker</id>
      <mirrorOf>external:http:*</mirrorOf>
      <name>Pseudo repository to mirror external repositories initially using HTTP.</name>
      <url>http://0.0.0.0/</url>
      <blocked>false</blocked>
    </mirror>
  <mirror>
      <id>my_nexus_server</id>
      <name>private nexus Repository</name>
      <url>http://localhost:8081/repository/maven-public/</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
</mirrors>
<servers>
  <server>
      <id>my_nexus_server</id>
      <username>admin</username>
      <password>yourpassword</password>
    </server>
</servers>

deploy library JAR to nexus

我們來創建一個utils專案來上傳到內部nexus server

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.5

發現套件已從自建的nexus server下載
https://ithelp.ithome.com.tw/upload/images/20251008/20128084vcWBkyqqRC.png
add Class

public class MyUtils {
    public String getMyUtilsHello() {
        return "Hello World!";
    }
}

pom.xml設定

<build>
  <distributionManagement>
    <repository>
      <id>my_nexus_server</id>
      <name>private nexus Repository</name>
      <url>http://localhost:8081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
      <id>my_nexus_server</id>
      <name>private nexus Repository</name>
      <url>http://localhost:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
  </distributionManagement>
</build>

透過deploy指令將JAR部署到nexus server,他會根據你的版本號是SNAPSHOT或是RELEASE部署到對應的repository

mvn deploy

在nexus server上就可以看到部署的JAR
https://ithelp.ithome.com.tw/upload/images/20251008/20128084pWcX4svuf3.png

use your team library JAR from nexus

create new maven project and try download dependency from local nexus server
我們來創建一個utils專案來上傳到內部nexus server

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.5

pom.xml設定

<dependency>
  <groupId>com.mycompany.myutils</groupId>
  <artifactId>myutils</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

java code

public class App {
    public static void main(String[] args) {

        System.out.println(new MyUtils().getMyUtilsHello());
    }
}

執行打包

mvn clean package

https://ithelp.ithome.com.tw/upload/images/20251008/20128084AIMBhOvzxf.png

淺談版本管理

我們來看一下maven repository上面的其中一個套件的naming,來了解套件命名的規範
https://ithelp.ithome.com.tw/upload/images/20251008/20128084tJTwzlzNy0.png

語義化版本(Semantic Versioning)

你會看到 12.X.XX,甚至後面有帶 alphabeta 的字眼,這遵循語義化版本規範:

基本格式:

<主要版本 major>.<次版本 minor>.<修補版本 patch>[-<預發布版本>]
  1. Major(主要版本):當有不相容的 API 變更時遞增

    • 例如:1.0.02.0.0
    • 表示可能有 Breaking Changes
  2. Minor(次版本):當有向下相容的新功能時遞增

    • 例如:1.1.01.2.0
    • 保持 API 相容性
  3. Patch(修補版本):當有向下相容的問題修復時遞增

    • 例如:1.1.11.1.2
    • 只修復 Bug,不新增功能
  4. Pre-release(預發布版本):可選的標識符

    • alpha:內部測試版本
    • beta:公開測試版本
    • rc or cr(Release Candidate):發布候選版本
    • SNAPSHOT:正在開發版本,可能隨時有變動
    • RELEASE :最終正式版本,套件不會再更動
    • m、milestone:此版本達成了一個重要的開發里程碑,包含大量新功能。

小結

本日介紹從如何安裝nexus server,到Library JAR部署至nexus以及演示我們部署的Library JAR讓其它專案引用,並簡單說明套件版本naming的規則。

Referencd


上一篇
Day22 - JAR Packaging:Native
系列文
我們與Maven的距離24
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言